<template>
  <div class='container'>
  <el-card class="box-card" >
    <!-- 面包屑 -->
  <div slot="header" class="clearfix" v-if="subjects">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item >学科管理</el-breadcrumb-item>
      <el-breadcrumb-item >{{$route.query.name}}</el-breadcrumb-item>
      <el-breadcrumb-item>学科标签</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <!-- 搜索框 -->
  <div>
  <el-row>
    <el-col :span="18">
      <el-form inline size="small" :model="form">
        <el-form-item label="标签名称">
          <el-input v-model="form.tagName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option
              v-for="item in stateList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item >
          <el-button @click="clear">清除</el-button>
          <el-button  type="primary" @click="searchList">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-col :span="6">
      <div class="addbtn">

        <el-button type="success"
          size="small"
          icon="el-icon-edit"
          style="float: right;"
          @click="showDialog=true"
          >新增目录</el-button>
           <el-button type="text"
          style="float: right;margin-right:5px;"
          v-if="subjects"
          @click="$router.back()"
          class="el-icon-back"
          >
          返回学科
        </el-button>
      </div>
    </el-col>
  </el-row>
  </div>
  <!-- 提示消息 -->
    <el-alert
      :title='alertText'
      type="info"
      show-icon
      :closable = false >
    </el-alert>
  <!-- 表单 -->
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%">
      <el-table-column
        type=index
        :index="indexMethod"
        width="80"
        align="center"
        label="序号"
        >
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="tagName"
        align="center"
        label="标签名">
      </el-table-column>
      <el-table-column
        prop="username"
        align="center"
        label="创建者">
      </el-table-column>

      <el-table-column
        prop="addDate | parseTime"
        align="center"
        label="创建日期">
        <template v-slot="{row}">
         {{row.addDate | parseTime }}
       </template>
      </el-table-column>
      <el-table-column
        prop="state"
        align="center"
        label="状态">
        <template v-slot='{row}'>
          <span>{{row.state===1 ? '已启用':'已禁用'}} </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <template v-slot='{row}'>
            <el-button type="text" size="samll" @click="changeBtn(row)">
              {{row.state===1 ? '禁用':'启用'}}
            </el-button>
            <el-button type="text" size="samll" :disabled='row.state===0' @click="edit(row)" >修改</el-button>
            <el-button type="text" size="samll" :disabled='row.state===0' @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
     <div class="block">
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Number(form.page)"
      :page-sizes="[5,10,20,30]"
      :page-size="Number(form.pagesize)"
      layout=" prev, pager, next,sizes, jumper"
      :total="Number(total)">
    </el-pagination>
  </div>
  </el-card>
   <TagsAdd :show-dialog.sync='showDialog' @getList='getList' ref='tagsId' :subjects='subjects'></TagsAdd>

  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags.js'
import TagsAdd from '@/module-hmmm/components/TagsAdd'
import { status } from '@/api/hmmm/constants.js'

export default {
  name: 'tags',
  components: {
    TagsAdd
  },
  data () {
    return {
      loading: false,
      // 添加框隐藏
      form: {
        tagName: null,
        state: null,
        subjectID: null,
        pagesize: 10, // 显示条数
        page: 1
      },
      showDialog: false,
      list: [],
      stateList: status,
      // 分页
      total: 0, // 储存总数量
      alertText: '' // 提示信息
    }
  },
  created () {
    // 渲染列表
    this.getList()
  },
  computed: {
    status () {
      return status
    },
    subjects () {
      return this.$route.query.id
    }
  },
  watch: {
    subjects () {
      this.TagsList()
    }
  },
  methods: {
    // 序列号
    indexMethod (index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1
    },
    handleSizeChange (pagesize) {
      this.form.page = 1
      this.form.pagesize = pagesize
      this.getList()
    },
    handleCurrentChange (page) {
      this.form.page = page
      this.getList()
    },
    TagsList () {
      if (this.subjects) {
        this.form.subjectID = this.subjects
        this.getList()
      } else {
        this.form.subjectID = null
        this.getList()
      }
    },
    // 渲染列表
    async getList () {
      this.loading = true
      if (this.subjects) {
        this.form.subjectID = this.subjects
      }
      const { data } = await list(this.form)
      // console.log(data)currentChange
      this.list = data.items
      // 获取总条数
      this.total = data.counts
      this.alertText = `共 ${this.total} 条记录`
      this.loading = false
    },
    // 点击清空输入框
    clear () {
      this.form = {
        tagName: null,
        state: null,
        subjectID: null,
        pagesize: 10, // 显示条数
        page: 1
      }
      // 重新渲染列表
      this.getList()
    },
    async del (row) {
      // console.log('123')
      try {
        await this.$confirm('你确定要删除吗？', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      // 发送请求删除
      await remove(row)
      this.$message.success('删除成功')
      // 重新渲染列表
      this.getList()
    },
    // 启用禁用切换
    async changeBtn (row) {
      row.state = row.state === 1 ? 0 : 1
      await changeState(row)
      this.$message.success('操作成功')
    },
    async edit (row) {
      this.showDialog = true
      this.$refs.tagsId.form = { ...row }
    },
    // 搜索
    searchList () {
      this.form.page = 1
      this.getList()
    }
  }
}
</script>

<style scoped lang='scss'>
.el-alert {
  margin: 15px 0;
}
.block{
  text-align: right;
  // height: 30px;
  margin-top: 20px;
}
</style>
